package beans;

import entities.AgeRating;
import java.util.Collection;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class AgeRatingFacade extends AbstractFacade<AgeRating> {
    @PersistenceContext(unitName = "ProjetAppliWebPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public AgeRatingFacade() {
        super(AgeRating.class);
    }

    public void createAll(ImageFacade imageFacade) {
        AgeRating earlyChildhood = getOrCreate(0, "Early Childhood");
        earlyChildhood.setDescription("Titles rated <strong>EC (Early Childhood)</strong> have content that may be suitable for ages 3 and older. Contains no material that parents would find inappropriate.");
        earlyChildhood.setImage(imageFacade.create("images/ratings/ec.gif", 60, 89));
        edit(earlyChildhood);

        AgeRating everyone = getOrCreate(1, "Everyone");
        everyone.setDescription("Titles rated <strong>E (Everyone)</strong> have content that may be suitable for ages 6 and older. Titles in this category may contain minimal cartoon, fantasy or mild violence and/or infrequent use of mild language.");
        everyone.setImage(imageFacade.create("images/ratings/e.gif", 60, 89));
        edit(everyone);

        AgeRating everyone10 = getOrCreate(2, "Everyone 10+");
        everyone10.setDescription("Titles rated <strong>E10+ (Everyone 10 and older)</strong> have content that may be suitable for ages 10 and older. Titles in this category may contain more cartoon, fantasy or mild violence, mild language and/or minimal suggestive themes.");
        everyone10.setImage(imageFacade.create("images/ratings/e10.gif", 60, 89));
        edit(everyone10);

        AgeRating teen = getOrCreate(3, "Teen");
        teen.setDescription("Titles rated <strong>T (Teen)</strong> have content that may be suitable for ages 13 and older. Titles in this category may contain violence, suggestive themes, crude humor, minimal blood, simulated gambling, and/or infrequent use of strong language. ");
        teen.setImage(imageFacade.create("images/ratings/t.gif", 60, 89));
        edit(teen);

        AgeRating mature = getOrCreate(4, "Mature");
        mature.setDescription("Titles rated <strong>M (Mature)</strong> have content that may be suitable for persons ages 17 and older. Titles in this category may contain intense violence, blood and gore, sexual content and/or strong language.");
        mature.setImage(imageFacade.create("images/ratings/m.gif", 60, 89));
        edit(mature);

        AgeRating adultsOnly = getOrCreate(5, "Adults Only");
        adultsOnly.setDescription("Titles rated <strong>AO (Adults Only)</strong> have content that should only be played by persons 18 years and older. Titles in this category may include prolonged scenes of intense violence and/or graphic sexual content and nudity.");
        adultsOnly.setImage(imageFacade.create("images/ratings/ao.gif", 60, 89));
        edit(adultsOnly);

        AgeRating ratingPending = getOrCreate(6, "Rating Pending");
        ratingPending.setDescription("Titles listed as <strong>RP (Rating Pending)</strong> have been submitted to the ESRB and are awaiting final rating. (This symbol appears only in advertising prior to a game's release.)");
        ratingPending.setImage(imageFacade.create("images/ratings/rp.gif", 60, 89));
        edit(ratingPending);
    }

    public AgeRating getByName(String name) {
        Query query = em.createQuery("SELECT ar FROM AgeRating ar WHERE ar.name = :name");
        query.setParameter("name", name);

        try {
            return (AgeRating) query.getSingleResult();
        } catch(NoResultException e) {
            return null;
        }
    }

    public AgeRating getOrCreate(int showOrder, String name) {
        AgeRating ageRating = getByName(name);

        if (ageRating == null) {
            ageRating = new AgeRating(showOrder, name);
            create(ageRating);
        }

        return ageRating;
    }

    public Collection<AgeRating> getAll() {
        Query query = em.createQuery("SELECT ar FROM AgeRating ar ORDER BY ar.showOrder");
        return query.getResultList();
    }
}